<template>

    <a-form ref="formRef" name="custom-validation" :model="formState" v-bind="layout" @finish="handleFinish">
        <a-row>

            <a-col :span="4">
                <a-form-item has-feedback label="项目分类" name="category_name">
                    <a-input allowClear v-model:value="formState.category_name" />
                </a-form-item>
            </a-col>

            <a-col :span="4">
                <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
                    <a-button type="primary" html-type="submit">查询</a-button>
                </a-form-item>
            </a-col>
        </a-row>

        <a-row>
            <a-col :span="24">
                <a-flex justify="flex-end">
                    <a-button type="primary" style="margin-left: 10px" @click="handleAdd">新增</a-button>
                </a-flex>
            </a-col>
        </a-row>
    </a-form>


    <a-row>
        <a-col :span="24">
            <a-table size="small" style="width: 100%;" :columns="columns" :data-source="data">
                <template #bodyCell="{ column, text, record }">
                    <template v-if="column.dataIndex === 'name'">
                        <a>{{ text }}</a>
                    </template>
                    <template v-if="column.dataIndex === 'action'">
                        <a type="primary" @click="handleEdit(record)">编辑</a>
                        <a type="danger" @click="deleteProjectCategory(record)">删除</a>
                    </template>
                </template>
            </a-table>
        </a-col>

    </a-row>

    <a-row>
        <a-drawer v-model:open="open" class="custom-class" root-class-name="root-class-name" style="color: red"
            :title="addFormState.category_id ? '编辑' : '新增'" placement="right">

            <a-form :rules="addRulesForm" ref="addFormRef" name="custom-validation" :model="addFormState"
                v-bind="layout" @finish="addFormState.category_id ? handleEditFinish($event) : handleAddFinish($event)">

                <a-form-item has-feedback label="项目类别" name="category_type">
                    <a-input v-model:value="addFormState.category_type" />
                </a-form-item>

                <a-form-item has-feedback label="类别描述" name="category_name">
                    <a-input v-model:value="addFormState.category_name" />
                </a-form-item>

                <a-form-item has-feedback>
                    <a-col :span="24">
                        <a-flex justify="flex-end">
                            <a-button type="primary" style="margin-left: 10px" html-type="submit">保存</a-button>
                        </a-flex>
                    </a-col>
                </a-form-item>
            </a-form>
        </a-drawer>
    </a-row>
</template>
<script setup>
import { project_category as service } from '@/api/modulesApi/project_category'
import { message } from 'ant-design-vue'

const columns = [
    {
        title: '项目分类',
        dataIndex: 'category_type',
        key: 'category_type',
    },
    {
        title: '类别描述',
        dataIndex: 'category_name',
        key: 'category_name',
    },
    {
        title: '创建时间',
        dataIndex: 'create_time',
        key: 'create_time',
    },
    {
        title: '创建人',
        dataIndex: 'creator',
        key: 'creator',
    },
    {
        title: '操作',
        dataIndex: 'action',
        key: 'action',
    },
];

const data = ref([]);


import { reactive, ref } from 'vue';
const formRef = ref();
const formState = reactive({
    category_name: '',
});


const open = ref(false);

const addFormState = reactive({
    category_type: '',
    category_name: '',
    category_id: '',
});


const addFormRef = ref();

const addRulesForm = {
    category_type: [{ required: true, message: '请输入项目类别' }],
};



const handleAdd = () => {
    addFormState.category_id = '';
    addFormState.category_name = '';
    addFormState.category_type = '';
    open.value = true;
};

const handleAddFinish = async (values) => {
    console.log(values, addFormState);
    let res = await service.addProjectCategory(values)
    message.success('保存成功')
    getProjectCategory()
    addFormRef.value.resetFields();
    open.value = false;
};

const layout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 14 },
};

const handleFinish = (values) => {
    console.log(values, formState);
    getProjectCategory()
};



const getProjectCategory = async (values) => {
    const res = await service.getProjectCategory(formState)
    console.log(res)
    data.value = res.data
}

getProjectCategory()


const deleteProjectCategory = async (row) => {
    console.log(row.category_id);

    const res = await service.deleteProjectCategory({ category_id: row.category_id })
    message.success('删除成功')
    getProjectCategory()
}

const handleEdit = (row) => {
    console.log(row);
    addFormState.category_type = row.category_type;
    addFormState.category_name = row.category_name;
    addFormState.category_id = row.category_id;
    open.value = true;
}

const handleEditFinish = async (values) => {
    values.category_id = addFormState.category_id;
    let res = await service.updateProjectCategory(values)
    message.success('保存成功')
    getProjectCategory()
    addFormRef.value.resetFields();
    open.value = false;
}


</script>